import 'package:sow_app_core/sow_app_core.dart';
import 'package:sow_app_core/src/usecase/cache_usecase.dart';
import 'package:sow_app_i18n/sow_app_i18n.dart';

import 'novel_usecase.dart';
import 'usecase.dart';

class SearchByTextUseCase extends UseCase<Future<void>, String> {
  final SearchPresenter _presenter;
  final NovelRepository _novelRepository;
  final CacheRepository _cacheRepository;

  SearchByTextUseCase(
      this._presenter, this._novelRepository, this._cacheRepository);
  @override
  Future<void> call(String params) async {
    Set<String> searchHistorySet =
        (await GetCacheUseCase(_cacheRepository)(I18n.search_history))
            .split("\\,")
            .toSet();
    searchHistorySet.add(params);
    await SetCacheUseCase(_cacheRepository)(
        SetCacheParams(I18n.search_history, searchHistorySet.join("\\,")));
    _presenter.searchNovelList = await GetNovelListUseCase(_novelRepository)(
        GetNovelListParams(params, null, null, null));
    _presenter.searchList = searchHistorySet.map((e) => Search(e)).toList();
    _presenter.onComplete();
  }
}
